<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>OSS文件上传</title>
    <!-- 引入jQuery -->
    <script src="../templates/h5/assets/jquery.min.js"></script>
    <script src="../templates/h5/assets/aliyun-oss-sdk-6.18.0.min.js"></script>
    <script src="../templates/h5/assets/vconsole.min.js"></script>
    <script src="../templates/h5/assets/ossUpload.js"></script>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f7fa;
            color: #333;
        }
        .container {
            background-color: white;
            border-radius: 10px;
            padding: 25px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        h1 {
            color: #1e88e5;
            text-align: center;
            margin-bottom: 30px;
        }
        .upload-area {
            border: 2px dashed #1e88e5;
            border-radius: 8px;
            padding: 30px;
            text-align: center;
            margin-bottom: 20px;
            background-color: #f8fdff;
            transition: all 0.3s;
            cursor: pointer;
        }
        .upload-area:hover {
            background-color: #e3f2fd;
            border-color: #1565c0;
        }
        .upload-area.highlight {
            background-color: #e3f2fd;
            border-color: #1565c0;
            transform: scale(1.01);
        }
        .file-input {
            display: none;
        }
        .upload-btn {
            background-color: #1e88e5;
            color: white;
            border: none;
            padding: 12px 25px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            font-weight: 600;
            transition: background-color 0.3s;
            margin-top: 15px;
            width: 100%;
        }
        .upload-btn:hover {
            background-color: #1565c0;
        }
        .upload-btn:disabled {
            background-color: #90caf9;
            cursor: not-allowed;
        }
        .file-info {
            margin-top: 15px;
            padding: 10px;
            background-color: #e3f2fd;
            border-radius: 5px;
            display: none;
        }
        .progress-container {
            margin-top: 20px;
            display: none;
        }
        .progress-bar {
            height: 20px;
            background-color: #e0e0e0;
            border-radius: 10px;
            overflow: hidden;
            margin-bottom: 10px;
        }
        .progress {
            height: 100%;
            background-color: #1e88e5;
            width: 0%;
            transition: width 0.3s;
        }
        .message {
            margin-top: 15px;
            padding: 10px;
            border-radius: 5px;
            text-align: center;
        }
        .info {
            background-color: #e3f2fd;
            color: #0d47a1;
        }
        .success {
            background-color: #e8f5e9;
            color: #2e7d32;
        }
        .error {
            background-color: #ffebee;
            color: #c62828;
        }
        .warning {
            background-color: #fff8e1;
            color: #f57f17;
        }
        .status {
            margin-top: 15px;
            font-weight: 500;
        }
        .icon {
            font-size: 48px;
            color: #1e88e5;
            margin-bottom: 15px;
        }
        .debug-info {
            margin-top: 20px;
            padding: 15px;
            background-color: #f5f5f5;
            border-radius: 5px;
            font-size: 12px;
            display: none;
        }
        .upload-result {
            margin-top: 20px;
            padding: 15px;
            background-color: #f8f9fa;
            border-radius: 5px;
            border-left: 4px solid #1e88e5;
            display: none;
        }
        .result-item {
            margin: 5px 0;
            display: flex;
        }
        .result-label {
            font-weight: 600;
            min-width: 100px;
            color: #555;
        }
        .result-value {
            flex: 1;
            word-break: break-all;
        }
        .copy-btn {
            background: #1e88e5;
            color: white;
            border: none;
            padding: 2px 8px;
            border-radius: 3px;
            cursor: pointer;
            font-size: 12px;
            margin-left: 10px;
        }
        .copy-btn:hover {
            background: #1565c0;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>OSS文件上传</h1>

    <div class="upload-area" id="uploadArea">
        <div class="icon">📁</div>
        <h3>点击选择文件或拖拽文件到此处</h3>
        <p>支持单个文件上传，最大文件大小为5GB</p>
        <input type="file" id="file" class="file-input"/>
    </div>

    <div class="file-info" id="fileInfo"></div>

    <div class="progress-container" id="progressContainer">
        <div class="progress-bar">
            <div class="progress" id="progressBar"></div>
        </div>
        <div class="status" id="progressText">0%</div>
    </div>

    <button class="upload-btn" id="submit" disabled>开始上传</button>

    <div class="message" id="message"></div>

    <!-- 上传结果展示区域 -->
    <div class="upload-result" id="uploadResult">
        <h3>上传结果</h3>
        <div class="result-item">
            <span class="result-label">文件名:</span>
            <span class="result-value" id="resultFileName">-</span>
        </div>
        <div class="result-item">
            <span class="result-label">文件URL:</span>
            <span class="result-value" id="resultFileUrl">-</span>
            <button class="copy-btn" onclick="copyToClipboard('resultFileUrl')">复制</button>
        </div>
        <div class="result-item">
            <span class="result-label">文件大小:</span>
            <span class="result-value" id="resultFileSize">-</span>
        </div>
        <div class="result-item">
            <span class="result-label">上传时间:</span>
            <span class="result-value" id="resultUploadTime">-</span>
        </div>
        <div class="result-item">
            <span class="result-label">ETag:</span>
            <span class="result-value" id="resultETag">-</span>
            <button class="copy-btn" onclick="copyToClipboard('resultETag')">复制</button>
        </div>
        <div class="result-item">
            <span class="result-label">请求ID:</span>
            <span class="result-value" id="resultRequestId">-</span>
        </div>
    </div>

    <div class="debug-info" id="debugInfo"></div>

    <button id="toggleDebug" style="margin-top: 10px; background: none; border: none; color: #666; cursor: pointer;">
        显示调试信息
    </button>
</div>

<script type="text/javascript">
    // 复制到剪贴板功能
    function copyToClipboard(elementId) {
        const element = document.getElementById(elementId);
        const text = element.textContent;

        navigator.clipboard.writeText(text).then(() => {
            alert('已复制到剪贴板: ' + text);
        }).catch(err => {
            console.error('复制失败:', err);
        });
    }



    // 使用示例
    $(document).ready(function() {
        // 初始化vConsole
        // var vConsole = new VConsole();
        // console.log('Hello world');

        // 初始化上传插件
        $('#file').ossUpload({
            stsUrl: '/oss/getStsToken',
            dropArea: '#uploadArea',
            messageContainer: '#message',
            progressContainer: '#progressContainer',
            progressBar: '#progressBar',
            progressText: '#progressText',
            resultContainer: '#uploadResult',
            autoUpload: false,
            onFileSelect: function(file) {
                const fileSize = (file.size / 1024 / 1024).toFixed(2);
                $('#fileInfo').html(`
                    <strong>文件信息:</strong><br>
                    名称: ${file.name}<br>
                    大小: ${fileSize} MB<br>
                    类型: ${file.type || '未知'}
                `).show();
                $('#submit').prop('disabled', false);
            },
            onProgress: function(percentage) {
                console.log('上传进度:', percentage);
            },
            onSuccess: function(result, fileName, uploadInfo) {
                console.log('上传成功:', uploadInfo);
                $('#submit').prop('disabled', true);
                const uploader = $('#file').getOSSUploader();
                uploader.saveToHistory(uploadInfo);
            },
            onError: function(error) {
                console.error('上传错误:', error);
                $('#submit').prop('disabled', false);
            },
            onResult: function(uploadInfo) {
                console.log('上传结果:', uploadInfo);
            }
        });

        // 上传按钮点击事件
        $('#submit').on('click', function() {
            const uploader = $('#file').getOSSUploader();
            if (uploader) {
                uploader.upload().then(uploadInfo => {
                    // 上传成功后的额外处理
                    console.log('上传完成，返回的信息:', uploadInfo);
                }).catch(error => {
                    console.error('上传失败:', error);
                });
            }
        });

        // 调试信息切换
        $('#toggleDebug').on('click', function() {
            const debugEl = $('#debugInfo');
            if (debugEl.is(':visible')) {
                debugEl.hide();
                $(this).text('显示调试信息');
            } else {
                debugEl.show();
                $(this).text('隐藏调试信息');
            }
        });
    });
</script>
</body>
</html>